package com.vvlive.caibao.smartcity.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.MyLocationBean;
import com.vvlive.caibao.smartcity.bean.VisitCustomer;
import com.vvlive.caibao.smartcity.utils.MyLocationUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import org.kymjs.kjframe.ui.BindView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by android on 2015/7/20.
 * 临时拜访
 */
public class TemporaryVisitCustomer extends BaseActivity implements MyLocationUtil.GetMyLocation {
    @BindView(id = R.id.chose_customer)
    private Spinner Schose_customer;
    @BindView(id = R.id.custom_adress_icon)
    private ImageButton Icon;
    @BindView(id= R.id.cu_address)
    private TextView Cu_adress;
    @BindView(id  = R.id.photo, click = true)
    private ImageView Take_photo;
    @BindView(id= R.id.cu_input)
    private EditText Spot;
    @BindView(id = R.id.cu_submit,click = true)
    private Button CuSbumit;
    @BindView(id = R.id.cu_about,click = true)
    private TextView Cuabout;
    @BindView(id = R.id.c_dingwei,click = true)
    private RelativeLayout layout;

    private String adress;
    private String string;
    private String type;
    private Uri fileUri;
    private static final int TAKE_PHOTO=1;

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_temporaryvisit;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar bar = getCommonActionBar();
        bar.setTitle("临时拜访");
        bar.setMoreVisible(false);
        ChoseCustomerType();
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()){
            case R.id.cu_about:
                CuAbout();
                break;
            case R.id.cu_submit:
                Submit();
                break;
            case R.id.photo:
                TakePhoto();
                break;
            case R.id.c_dingwei:
                Dingwei();
                break;
            default:
                break;
        }
    }
    private boolean Judge(){
        return  !TextUtils.isEmpty(adress)&&!TextUtils.isEmpty(string)&&!TextUtils.isEmpty(type)?true:false;
    }
    private void Submit(){
       try {
           string = Spot.getText().toString();
       }catch (Exception e){
           Log.e("zhi",e.toString());
           e.printStackTrace();

       }

        if (Judge()){
            alert(new VisitCustomer(adress,string,type));
        }else {
            alert("请完善临时拜访的信息");
        }

    }
    private void CuAbout(){
        alert("路费不报销");
    }
    private void TakePhoto(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, TAKE_PHOTO);

    }
    String[] strings = new String[]{"客户","客户","客户","客户","客户"};
    ArrayAdapter arrayAdapter ;
    private void  ChoseCustomerType(){
        arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,strings);
        Schose_customer.setAdapter(arrayAdapter);
      Schose_customer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              type = strings[position];
          }

          @Override
          public void onNothingSelected(AdapterView<?> parent) {

          }
      });
    }
    private void Dingwei(){
        new MyLocationUtil(this,this);
    }


    @Override
    public void MyLocation(MyLocationBean myLocationBean) {
        adress = myLocationBean.getRoad();
        Log.e("zhi",adress);
        Cu_adress.setText(adress);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK){
            /*String sdpath = Environment.getExternalStorageState();
            if (sdpath.equals(Environment.MEDIA_MOUNTED)){
                Log.e("zhi",
                        "SD card is not avaiable/writeable right now.");
                return;
            }*/
            String name = "smart"+".jpg";
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");
            FileOutputStream fileOutputStream = null;
            File file = new File(Environment.getExternalStorageDirectory()+"/smartcity/");
            file.mkdirs();
            String filename = Environment.getExternalStorageDirectory()+"/smartcity/"+name;

            try {
                fileOutputStream = new FileOutputStream(filename);
                bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
                Take_photo.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
